<?
/**
 * VoiceOne Settings/Queues
 *
 * Frontend to provide access to the queue system in voiceone.
 * @package voiceone
 * @subpackage  settings_queues
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang;
  require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  sajax_init();
  //================ XAJAX ================
  function chengeCategory($cat){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    if($cat=="QUEUES"){
      $optionArr=$asterisk->getConfOption(0,"QUEUES",SECTION_GENERAL);
      $html_option=getOption($optionArr,null,true,"");  
    }else if($cat=="AGENTS_GENERAL"){
      $optionArr=$asterisk->getConfOption(0,"AGENTS_GENERAL",SECTION_GENERAL);
      $html_option=getOption($optionArr,null,true,"");
    }else if($cat=="AGENTS_AGENTS"){
      $optionArr=$asterisk->getConfOption(0,"AGENTS_AGENTS",SECTION_GENERAL);
      $html_option.=getOption($optionArr,null,true,"");
    }
    $html_option="<table>".$html_option."</table>";
    // Return the xajaxResponse object
    $objResponse->addAssign("optionList","innerHTML",$html_option);
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("chengeCategory");
  $xajax->processRequests();
  
  //================== POST ====================
  if(count($_POST)>0){
    //extract the category
    $category=$_POST["category"];
    //extract options
    foreach ($_POST as $k=>$v) {
      if (substr($k,0,7)=="status_") {
        $k=substr($k,7);
        if ($v=="1") {
          $o["key"]=$k; 
          $o["value"]=$_POST["option_".$k];
          $options[]=$o;
        }
      }
    }
    //put options
    $ret=$asterisk->putQueueGeneralOption($category,$options);
    if($ret>0){
      $alert="<div class=\"AlertGood\">".T_("The queue has been modified")."</div>";
      $_GET["id"]=$ret;
    }else $alert="<div class=\"AlertBad\">".T_("The queue hasn't been modified")."</div>";
  }
  //================ PAGE LOGIC ===================
  //define init script
  if((isset($category))&&($category!="")) $initScript="<script> xajax_chengeCategory('".$category."'); </script>";
  else $initScript="<script> xajax_chengeCategory('QUEUES'); </script>";
  //define selected object on select list
  if($category=="AGENTS_GENERAL") $agselected="selected=\"selected\"";
  else if($category=="AGENTS_AGENTS") $aaselected="selected=\"selected\"";
  else $qselected="selected=\"selected\"";
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/");
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Queue Options")?></h1> 
  </div>
  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post">
      <h2><?=T_("Options")?></h2>
      <p class="blockintro"><?=T_("Choose category")?>: 
        <select id="category" name="category" onchange="xajax_chengeCategory(this.value);">
          <option value="QUEUES" <?=$qselected?> ><?=T_("Queue General")?></option>
          <option value="AGENTS_GENERAL" <?=$agselected?> ><?=T_("Agents General")?></option>
          <option value="AGENTS_AGENTS" <?=$aaselected?> ><?=T_("Agents Advanced")?></option>
        </select>
      </p>
      <div id="optionList" class="block">
        <table><?=$html_option?></table>
      </div>
      <div class="action">
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
      </div>
    </form>
  </div>
  <?=$initScript?>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="queueoptions";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("To be done...")?></p>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
  $smarty->assign("section", "queueoptions");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>
